<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Auth;
use app\common\business\GrabSheet;
use app\common\business\PlaceOrder as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use think\App;
use think\Exception;

class Order extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit', 10, 'intval');
            $relname = $this->request->param('relname','','trim');
            $mobile = $this->request->param('mobile','','trim');
            $classify = $this->request->param('classify','','trim');
            if(!empty($classify)){
                list($start,$end) = explode('~',$classify);
                $map[] = [
                    ['create_time', 'between', [
                        strtotime($start),strtotime($end)
                    ]]
                ];
            }else {
                $map[] = [
                    ['create_time', 'between', Time::month()]
                ];
            }
            if(!empty($relname)){
                $map[] = [
                    ['relname','=',$relname]
                ];
            }
            if(!empty($mobile)){
                $map[] = [
                    ['mobile','=',$mobile]
                ];
            }
            try {
                $lists = $this->busObj->getIndexLimitPage($limit, $map, 'id desc', 'id,relname,sex as relSex,mobile,wx,express_order,money,state,payStatus,create_time,end_address,address_detail,order_id,start_address as site,start_address,address_qu_info');
            } catch (\Exception $e) {
                return Show::json_show(Status::status_error, "发生错误");
            }
            return Show::json_show(Status::status_normal, "ok", $lists['data'], $lists['total']);
        }
        $site = (new \app\common\business\Site())->getListData();
        $this->assign('site',$site);
        $month = Time::month();
        $this->assign('start_time',date('Y-m-d H:i:s',$month[0]));
        $this->assign('end_time',date('Y-m-d H:i:s',$month[1]));
        return $this->fetch();
    }

    /**
     * @return string
     * @throws Exception
     */
    public function open()
    {
        $id = $this->request->param('id',0,'intval');
        if($id == 0){
            throw new Exception("发生错误");
        }
        try {
            $detail = $this->busObj->getBaseById($id,'*,sex as setsex,weight as set_weight,xian as set_xian,description as set_desc,is_lou as set_lou,start_address as site');
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return $this->fetch('',[
            'detail'=>$detail
        ]);
    }
    public function detail(){
        $id = $this->request->param('id',0,'intval');
        if($id == 0){
            return  Show::json_show(Status::status_error,"订单查询错误");
        }
        try {
            $order = $this->busObj->getBaseById($id);
        }catch (\Exception $e){
            return Show::json_show(Status::status_error,"订单失败");
        }
        if(!$order){
            throw new Exception("订单不存在");
        }
        if($order['state'] == 1){
            throw new Exception("订单还未接单");
        }
        try {
            $grabSheet = (new GrabSheet())->getGrabSheetByOrderId($order['id']);
        }catch (\Exception $e){
            return Show::json_show(Status::status_error,$e->getMessage());
        }
        if(!$grabSheet){
            return Show::json_show(Status::status_error,"数据不存在");
        }
        try {
            $auth = (new Auth())->getAuthByOpenid($grabSheet['openid'],'*,sex as set_sex');
        }catch (\Exception $e){
            throw new Exception("骑手不存在");
        }
        if(!$auth){
            throw new Exception("骑手不存在");
        }
        return $this->fetch('',[
            'auth'=>$auth
        ]);
    }
}